package com.mk.admin.service.impl;

import com.mk.admin.mapper.DoctorScienceInfoLikesMapper;
import com.mk.admin.mapper.DoctorScienceInfoMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.dto.DoctorScienceInfoLikesDto;
import com.mk.admin.model.entity.DoctorScienceInfoEntity;
import com.mk.admin.model.entity.DoctorScienceInfoLikesEntity;
import com.mk.admin.model.entity.NoticeEntity;
import com.mk.admin.model.query.DoctorScienceInfoQuery;
import com.mk.admin.model.vo.DoctorScienceInfoVo;
import com.mk.admin.service.DoctorScienceInfoService;
import com.mk.admin.util.DateTool;
import com.mk.admin.util.JSONCopy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DoctorScienceInfoServiceImpl implements DoctorScienceInfoService {
    @Autowired
    private DoctorScienceInfoMapper doctorScienceInfoMapper;
    @Autowired
    private DoctorScienceInfoLikesMapper doctorScienceInfoLikesMapper;

    @Override
    public ResultDto select(DoctorScienceInfoQuery query) {
        DoctorScienceInfoEntity entity = JSONCopy.copy(query, DoctorScienceInfoEntity.class);
        return ResultDto.data(doctorScienceInfoMapper.count(entity), doctorScienceInfoMapper.select(entity));
    }

    @Override
    public ResultDto add(DoctorScienceInfoVo doctorScienceInfoVo) {
        doctorScienceInfoVo.setCreateTime(DateTool.format());
        return ResultDto.data(doctorScienceInfoMapper.insert(JSONCopy.copy(doctorScienceInfoVo, DoctorScienceInfoEntity.class)));
    }

    @Override
    public ResultDto update(DoctorScienceInfoVo doctorScienceInfoVo) {
        return ResultDto.data(doctorScienceInfoMapper.updateById(JSONCopy.copy(doctorScienceInfoVo, DoctorScienceInfoEntity.class)));
    }

    @Override
    public ResultDto delete(int id) {
        return ResultDto.data(doctorScienceInfoMapper.deleteById(id));
    }


    @Override
    public ResultDto status(int id) {
        return ResultDto.data(doctorScienceInfoMapper.status(id));
    }

    /**
     * 浏览次数，浏览后在notice_like表里面进行记录
     *
     * @param vo
     * @return
     */
    @Transactional
    @Override
    public ResultDto times(DoctorScienceInfoVo vo) {
        //        根据公告通知的id和用户patId查询是否有值，如果有，则直接更新浏览次数，如果没有，则创建一条数据，并更新

        DoctorScienceInfoLikesEntity entity = new DoctorScienceInfoLikesEntity();
        entity.setInfoId(vo.getId());
        entity.setPatId(vo.getPatId());
        doctorScienceInfoMapper.times(JSONCopy.copy(vo, DoctorScienceInfoLikesEntity.class));
        List<DoctorScienceInfoLikesDto> list = doctorScienceInfoLikesMapper.select(entity);
        if (list.size() == 0) {

            entity.setStatusTimes(1);
            entity.setCreateTimeTimes(DateTool.format());
            doctorScienceInfoLikesMapper.insert(entity);
        } else {
            entity.setId(list.get(0).getLikesId());
            entity.setStatusTimes(1);
            entity.setCreateTimeTimes(DateTool.format());
            doctorScienceInfoLikesMapper.updateById(entity);
        }

        return ResultDto.data(doctorScienceInfoMapper.selectById(vo.getId()));
    }

    /**
     * 点赞次数，点赞后在notice_like表里面进行记录
     *
     * @param vo
     * @return
     */
    @Transactional
    @Override
    public ResultDto likes(DoctorScienceInfoVo vo) {

        DoctorScienceInfoLikesEntity entity = new DoctorScienceInfoLikesEntity();
        entity.setInfoId(vo.getId());
        entity.setPatId(vo.getPatId());
        doctorScienceInfoMapper.likes(JSONCopy.copy(vo, DoctorScienceInfoLikesEntity.class));
        List<DoctorScienceInfoLikesDto> list = doctorScienceInfoLikesMapper.select(entity);
        if (list.size() == 0) {
            entity.setStatusLikes(vo.getNum() == 1 ? 1 : 0);
            entity.setCreateTimeLikes(DateTool.format());
            doctorScienceInfoLikesMapper.insert(entity);
        } else {
            entity.setId(list.get(0).getLikesId());
            entity.setStatusLikes(vo.getNum() == 1 ? 1 : 0);
            entity.setCreateTimeLikes(DateTool.format());
            doctorScienceInfoLikesMapper.updateById(entity);
        }

        return ResultDto.data("success");
    }

    @Transactional
    @Override
    public ResultDto collects(DoctorScienceInfoVo vo) {

        DoctorScienceInfoLikesEntity entity = new DoctorScienceInfoLikesEntity();
        entity.setInfoId(vo.getId());
        entity.setPatId(vo.getPatId());
        doctorScienceInfoMapper.collects(JSONCopy.copy(vo, DoctorScienceInfoLikesEntity.class));
        List<DoctorScienceInfoLikesDto> list = doctorScienceInfoLikesMapper.select(entity);
        if (list.size() == 0) {

            entity.setStatusCollects(vo.getNum() == 1 ? 1 : 0);
            entity.setCreateTimeCollects(DateTool.format());
            doctorScienceInfoLikesMapper.insert(entity);
        } else {
            entity.setId(list.get(0).getLikesId());
            entity.setStatusCollects(vo.getNum() == 1 ? 1 : 0);
            entity.setCreateTimeCollects(DateTool.format());
            doctorScienceInfoLikesMapper.updateById(entity);
        }

        return ResultDto.data("success");
    }

    @Transactional
    @Override
    public ResultDto forwards(DoctorScienceInfoVo vo) {

        DoctorScienceInfoLikesEntity entity = new DoctorScienceInfoLikesEntity();
        entity.setInfoId(vo.getId());
        entity.setPatId(vo.getPatId());
        List<DoctorScienceInfoLikesDto> list = doctorScienceInfoLikesMapper.select(entity);
        if (list.size() == 0) {
            doctorScienceInfoMapper.forwards(JSONCopy.copy(vo, DoctorScienceInfoLikesEntity.class));
            entity.setStatusForwards(vo.getNum() == 1 ? 1 : 0);
            entity.setCreateTimeForwards(DateTool.format());
            doctorScienceInfoLikesMapper.insert(entity);
        } else {
            entity.setId(list.get(0).getLikesId());
            entity.setStatusForwards(vo.getNum() == 1 ? 1 : 0);
            entity.setCreateTimeForwards(DateTool.format());
            doctorScienceInfoLikesMapper.updateById(entity);
        }

        return ResultDto.data("success");
    }

}
